package com.ideal.frame.node.netty.client;

import com.ideal.frame.node.netty.NettyMsg;
import io.netty.channel.Channel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @description:
 * @author: xuyang
 * @date: 2021/9/2 10:43
 */
public class NettyClientConstant {
    public static NettyMsg heartbeatMsg = null;
    public static Map<String, ChannelAndNodeId> channelMaps = new ConcurrentHashMap<>();
    public static List<String> cancelHeartbeatList = new ArrayList<>();
    public static Integer selfNodeLevel = null;

    /**
     * 记录客户端重连次数
     */
    public static Map<String, Long> retryConnTimesMap = new ConcurrentHashMap<>();
    // 客户端重连次数自增1次
    public static Long incrRetryConnTimes(String hostAndPort){
        Long times = NettyClientConstant.retryConnTimesMap.get(hostAndPort);
        if(times==null){
            times = 0L;
        }
        NettyClientConstant.retryConnTimesMap.put(hostAndPort, ++times);
        return times;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class ChannelAndNodeId{

        private Long nodeId;
        /**
         * 0原始根节点，-1根节点，1一级节点2二级节点3三级节点
         *
         */
        private Integer noteLevel;
        private Channel channel;
    }

}
